﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Models;
using System.Collections;
using WebHoiChoMuaBan.Controllers.Helpers;
using Mail;

namespace WebHoiChoMuaBan.Controllers.Admin
{
    public class MemberController : AdminController
    {
        public MemberController()
        {
            //loadCommon();
        }
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            if (!IsLogin)
                return HomeAdminPage();
            return ListNewMember(1);
        }

        //public ActionResult ListNewMember()
        //{
        //    return ListNewMember(1);
        //}y

        public ActionResult ListNewMember(int? page)
        {
            int currentPage = 1;
            //if (int.TryParse(Request.QueryString["page"], out currentPage) == false)
            if (page != null)
                currentPage = (int)page;
            MemberHelper hMem = new MemberHelper(MemberHelper.NO_ID);
            MemberList lstNewMembers = hMem.getNewMembers(currentPage, Config.ITEMS_PER_PAGE);

            lstNewMembers.CurrentPage = currentPage;
            return View("~/Views/Admin/Member/ListMember.aspx", lstNewMembers);
        }

        private delegate MemberList ListMemberDelegate(int page, int rowPerPage);
        private ActionResult ListMember(int? page, ListMemberDelegate delListMem)
        {
            int currentPage = 1;
            if (page != null)
                currentPage = (int)page;
            MemberHelper hMem = new MemberHelper();
            MemberList lstNewMembers = delListMem(currentPage, Config.ITEMS_PER_PAGE);

            lstNewMembers.CurrentPage = currentPage;
            return View("~/Views/Admin/Member/ListMember.aspx", lstNewMembers);
        }

        public ActionResult ListGoldMember(int? page)
        {
            MemberHelper hMem = new MemberHelper();
            return ListMember(page, new ListMemberDelegate(hMem.getGoldMembers));
        }

        public ActionResult ListLockedMember(int? page)
        {
            MemberHelper hMem = new MemberHelper();
            return ListMember(page, new ListMemberDelegate(hMem.getLockedMembers));
        }



        public ActionResult ListAllMember(int? page)
        {
            MemberHelper hMem = new MemberHelper();
            return ListMember(page, new ListMemberDelegate(hMem.getAllMembers));
        }

        public ActionResult ListActiveMember(int? page)
        {
            MemberHelper hMem = new MemberHelper();
            return ListMember(page, new ListMemberDelegate(hMem.getActiveMembers));
        }
        public ActionResult LockMember(int? id)
        {
            if (id != null)
            {
                MemberHelper hMem = new MemberHelper();
                Member mem = hMem.getMember((int)id);
                mem.MemStatus = (char)Member.Status.Locked;
                hMem.Save();

            }
            return this.Redirect(Request.UrlReferrer.PathAndQuery);
        }

        [HttpPost]
        public void DeleteMember(int id)
        {
            Member mem = new MemberHelper().getMemberforDelete(id);
            new MemberHelper().Delete(mem);
        }

        [HttpPost]
        public void SendMailMember(int id, string content)
        {
            Member mem = new MemberHelper().getMember(id);
            SendMail mail = new SendMail("smtp.gmail.com", 587, "Liên hệ thành viên từ http://hoichomuaban.com");
            List<string> noidung = new List<string>();
            noidung.Add(mem.MemFullname);
            noidung.Add(content);
            string filePath = Server.MapPath("~/Content/Templates/Emails/GoiMailThanhVien.txt");
            mail.sendmail("ibclubvlu@gmail.com", "vanlang123", mem.MemEmail, filePath, noidung);
        }

        public ActionResult ActivateMember(int? id)
        {
            if (id != null)
            {
                MemberHelper hMem = new MemberHelper();
                Member mem = hMem.getMember((int)id);
                mem.MemStatus = (char)Member.Status.Active;
                hMem.Save();

            }
            return this.Redirect(Request.UrlReferrer.PathAndQuery);
        }

        [HttpPost]
        public ActionResult AddUpPoint(FormCollection form)
        {
            int[] memIDs;
            int point;
            ArrayList lstErrors = checkPointParam(form, out memIDs, out point);

            if (lstErrors.Count == 0)
            {
                for (int i = 0; i < memIDs.Length; i++)
                {
                    AddUpPoint(memIDs[i], point);
                }
            }

            Session["lstError"] = lstErrors;
            return this.Redirect(Request.UrlReferrer.PathAndQuery);
        }


        [HttpPost]
        public ActionResult SubtractUpPoint(FormCollection form)
        {
            int[] memIDs;
            int point;
            ArrayList lstErrors = checkPointParam(form, out memIDs, out point);

            if (lstErrors.Count == 0)
            {
                for (int i = 0; i < memIDs.Length; i++)
                {
                    AddUpPoint(memIDs[i], -point);
                }
            }

            Session["lstError"] = lstErrors;
            return this.Redirect(Request.UrlReferrer.PathAndQuery);
        }

        private ArrayList checkPointParam(FormCollection form, out int[] memIDs, out int point)
        {
            ArrayList lstErrors = new ArrayList();
            memIDs = null;
            point = 0;
            string selMem = form.Get("selectedMem");
            if (selMem != null)
            {
                string[] strMemIDs = selMem.Split(',');
                memIDs = new int[strMemIDs.Length];
                //int uid;

                for (int i = 0; i < strMemIDs.Length; i++)
                {
                    if (!int.TryParse(strMemIDs[i], out memIDs[i]))
                    {
                        lstErrors.Add("Dữ liệu không hợp lệ.");
                        break;
                    }
                }

                point = 0;
                if (form.Get("inputNumber") == null || int.TryParse(form.Get("inputNumber"), out point) == false || point <= 0)
                    lstErrors.Add("Hãy nhập số điểm lớn hơn 0.");
            }
            else lstErrors.Add("Hãy chọn ít nhất một thành viên.");
            return lstErrors;
        }

        private void AddUpPoint(int id, int point)
        {
            MemberHelper hMem = new MemberHelper();
            Member mem = hMem.getMember(id);
            hMem.Refresh(mem);
            if (mem != null)
            {
                mem.MemUpPoint += point;
                if (mem.MemUpPoint < 0)
                    mem.MemUpPoint = 0;
                hMem.Save();
            }
        }

        public ActionResult CreateCategory()
        {
            return View();
        }

        public ActionResult CreateAdmin()
        {
            return View();
        }

        public ActionResult Product()
        {
            return View();
        }

        public ActionResult InvidialManagement()
        {
            return View();
        }

        public ActionResult Photo()
        {
            return View();
        }

        public ActionResult News()
        {
            return View();
        }

        public ActionResult MemberMail()
        {
            return View();
        }

        public ActionResult ListAdmin()
        {
            return View();
        }

        public ActionResult AddTemp()
        {
            return View();
        }
    }
}
